//
//  LLWelcomeViewController.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/29.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit
import SDWebImage

class LLWelcomeViewController: UIViewController {
    
    
    private var iconViewBpttonCons:NSLayoutConstraint?
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        
        iconViewBpttonCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        UIView.animateWithDuration(1.0, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.nameLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        (UIApplication.sharedApplication().delegate as!AppDelegate).switchRootController(true)
                })
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        // Do any additional setup after loading the view.
        
        if let urlSting = LLUserAccount.loadAccount()?.avatar_large{
            iconView.LL_setImageWithURL(NSURL(string: urlSting), placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
    }
    
    private func prepareUI(){
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
//        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        nameLabel.translatesAutoresizingMaskIntoConstraints = false
//        
//       view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : backgroundImageView]))
//        
//      view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : backgroundImageView]))
        
        backgroundImageView.ff_Fill(view)
        
        //头像
//       view.addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        iconViewBpttonCons = (NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
//        view.addConstraint(iconViewBpttonCons!)
//        
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
    let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        iconViewBpttonCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        //文字
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        
        nameLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
        
        
    }
    private lazy var backgroundImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        
        return imageView
    }()
    
    private lazy var iconView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.alpha = 0
        label.sizeToFit()
        return label
    }()
    
    
    

  

}
